package com.shanhai.wallpapers.fragments.Search

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.shanhai.wallpapers.databinding.FragmentSearchBinding
import com.shanhai.wallpapers.extensions.applyStatusBarPadding

class SearchFragment : Fragment() {
    private var mBinding: FragmentSearchBinding? = null
    private val binding get() = requireNotNull(mBinding)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        if (mBinding == null) {
            mBinding = FragmentSearchBinding.inflate(inflater, container, false)
            init()
        }
        return binding.root
    }

    private fun init() {
        setListeners()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.layoutHeader.applyStatusBarPadding()
    }

    private fun setListeners() {
        binding.imageBack.setOnClickListener {
            hideKeyBord()
            findNavController().popBackStack()
        }
    }

    private fun hideKeyBord() {
        val inputMethodManager =
            requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(binding.inputSearch.windowToken, 0)
    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding = null
    }
}